import React, { FC } from 'react'
import { Redirect, Route } from 'react-router-dom'
import { Auth } from '../store/models/auth'
import { isAuth } from '../utils/auth'

interface Props {
  path: string
  role: string | number
  component: FC
}

const AdminRoute: FC<Props> = ({ path, role, component }) => {
  function getComponent() {
    return isAuth() && (isAuth() as Auth).user.role === role
      ? component
      : () => <Redirect to='/signin' />
  }
  return <Route path={path} component={getComponent()} exact></Route>
}
export default AdminRoute
